/*

This do-file creates Appendix Tables S6 and S7. 

It uses "long" data from four waves:
2015, 2019, Apr 2020 and June 2020

and estimates differences between lottery winning and losing households pre- and post-COVID
*/



use "${data}/g2g_covid_income_long.dta", clear

loc eer_vars_S6 usd_win_inc app_work enough_food
loc eer_vars_S7 usd_win_inc_remit usd_win_inc_wages usd_win_inc_business usd_win_inc_home_prod


label var lottery_post "Migrant $\times$ COVID-19"
label var post "COVID-19"
label var lottery "Migrant"

foreach type in S6 S7 {
	
	eststo clear
	foreach var in `eer_vars_`type'' {
		
		sum `var' if lottery==0 & post==0
		loc pre_mean = `r(mean)'
		
	
		eststo: areg `var' lottery_post post lottery  i.round, ///
			absorb(upazila_Clean) cluster(union_Clean)
			estadd scalar crtl_mean = `pre_mean'
		
		
		eststo: areg `var' lottery_post post i.round, ///
			absorb(caseid) cluster(union_Clean) 
			estadd scalar crtl_mean = `pre_mean'
			estadd local fe_hh X
	}
	
	if "`type'"=="S6" {
		local col1 " Outcome: & \multicolumn{2}{c}{HH Income} & \multicolumn{2}{c}{Employed} & \multicolumn{2}{c}{Food Security} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}"
		local col2 " Comparison Period: & \multicolumn{2}{c}{2018, 2019} & \multicolumn{2}{c}{2018} & \multicolumn{2}{c}{2018} \\"
		local col3 " Source: & \multicolumn{2}{c}{Apr, Jun 2020} & \multicolumn{2}{c}{Jun 2020} & \multicolumn{2}{c}{Jun 2020} \\ \midrule"
		local bsty "b(1 1 2 2 3 3)"
	}
	else if "`type'"=="S7" {
		local col1 " Income Source: & \multicolumn{2}{c}{Remittances} & \multicolumn{2}{c}{Wages} & \multicolumn{2}{c}{Microenterprises} & \multicolumn{2}{c}{Home Production} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule{8-9}"
		local col2 ""
		local col3 "" 
		local bsty "b(1)"
	}
	esttab using "$tables/Table_`type'.tex", keep(lottery_post post lottery) ///
		replace se tex label  mlabels(none) nonotes nonumbers nostar `bsty' /// starlevels(* 0.1 ** 0.05 *** 0.01)
		stats(fe_hh ctrl_mean r2 N, fmt(0 2 2 0) labels("HH FEs" "Prior Non-Migrant Mean" "R-Squared" "Observations")) ///
		posthead(`col1' `col2' `col3')
}
